#!/usr/bin/env bash

# Sets the LD_LIBRARY_PATH required for running ITs dependent on libjvm.
# See CONTRIBUTING.md for details.

JAVA_HOME="${JAVA_HOME:-$(java -XshowSettings:properties -version \
    2>&1 > /dev/null |\
    grep 'java.home' |\
    awk '{print $3}')}"

if [[ "$(uname -s)" == "Darwin" ]]; then
    LIB_NAME="libjli"
else
    LIB_NAME="libjvm"
fi

# As JDK 8 and 9+ use different relative paths for libjvm, find the library:
LIBJVM_PATH="$(find "${JAVA_HOME}" -type f -name "${LIB_NAME}.*" -print0 -quit | xargs -0 -n1 dirname)"

export LD_LIBRARY_PATH="${LIBJVM_PATH}"

# on macOS, cargo use DYLD_FALLBACK_LIBRARY_PATH to locate dynamic libraries. 
# See https://doc.rust-lang.org/cargo/reference/environment-variables.html#dynamic-library-paths
export DYLD_FALLBACK_LIBRARY_PATH="${LIBJVM_PATH}":$DYLD_FALLBACK_LIBRARY_PATH
